package com.kuqi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.kuqi.R;
import com.kuqi.core.SysManager;
import com.kuqi.entitiy.Chapter;
import com.kuqi.entitiy.ReadSetting;
import java.util.ArrayList;


public class ChapterTitleAdapter extends ArrayAdapter<Chapter> {

    private int mResourceId;
    private ReadSetting setting;
    private int curChapterPosition = -1;

    public ChapterTitleAdapter(Context context, int resourceId, ArrayList<Chapter> datas){
        super(context,resourceId,datas);
        mResourceId = resourceId;
        setting = SysManager.getSetting();
    }

    @Override
    public void notifyDataSetChanged() {
        setting = SysManager.getSetting();
        super.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(mResourceId,null);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_chapter_title);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        initView(position,viewHolder);
        return convertView;
    }

    private void initView(int postion,final ViewHolder viewHolder){
        final  Chapter chapter = getItem(postion);
        viewHolder.tvTitle.setText("【" + chapter.getChapterName() + "】"); // 显示内容
        viewHolder.tvTitle.setTextColor(getContext().getResources().getColor(R.color.gray));
        if (postion == curChapterPosition){ // 如果显示的是当前选中的章节，则改变背景色
            viewHolder.tvTitle.setTextColor(getContext().getResources().getColor(R.color.higtlight));
        }

    }

    public void setCurChapterPosition(int curChapterPosition) {
        this.curChapterPosition = curChapterPosition;
    }

    public int getCurChapterPosition(){
        return this.curChapterPosition;
    }

    class ViewHolder{
        TextView tvTitle;
    }

}
